<?php
class PreviousObjectivesAction extends CAction {
	public function run() {
		$this->controller->layout = 'project';
		$category = "ItemTreeAction [P#".$_GET['project_id']." M#".$_GET['id']."]";
		Yii::log("Loading meeting #".$_GET['id'],CLogger::LEVEL_INFO, $category);
		$this->controller->layout = 'empty';
		$meeting = Meetings::model()->findByPk($_GET['id']);
		if ($meeting == null) {
			throw new CHttpException(404, "Meeting #".$_GET['id']." not found");
		}
		$this->controller->project = $meeting->project;
		$this->controller->pageTitle = $this->controller->project;

		$update = false;
		$topics = array();

		foreach($meeting->previousObjectives as $topic) {
			$topics[$topic->key] = $topic;
		}
		if (isset($_POST['next_button']) || isset($_POST['prev_button'])) {
			$update = true;
			if (isset($_POST['ProjectTopic'])) {
				Yii::log("Merging post data.",CLogger::LEVEL_INFO, $category);
				foreach ($_POST['ProjectTopic'] as $key => $topic) {
					$arr = $topic;
					$topics[$key]->attributes = $arr;
					unset($arr);
				}
				$stack = array_merge($topics);
				while (count($stack) > 0) {
					$topic = array_shift($stack);
					ProjectTopicHelper::adaptAllChildrenAndParentStatuses($topic);
					if ($topic->objective == ProjectTopic::YES) {
						if ($topic->objective_date == null)
							$topic->objective_date = $meeting->meeting_date;
					} else if ($topic->objective == ProjectTopic::INHERITS) {
						$topic->objective_date = $topic->parent->objective_date;
						$topic->objective_needs = null;
						$topic->objective_contacts = null;
					} else {
						$topic->objective_date = null;
						$topic->objective_needs = null;
						$topic->objective_contacts = null;
					}

					if ($topic->adviser_validation == ProjectTopic::YES) {
						if ($topic->adviser_validation_date == null)
							$topic->adviser_validation_date = $meeting->meeting_date;
					} else if ($topic->adviser_validation == ProjectTopic::INHERITS) {
						$topic->adviser_validation_date = $topic->parent->adviser_validation_date;
					} else {
						$topic->adviser_validation_date = null;
					}

					if ($topic->owner_validation == ProjectTopic::YES) {
						if ($topic->owner_validation_date == null)
							$topic->owner_validation_date = $meeting->meeting_date;
					} else if ($topic->owner_validation == ProjectTopic::INHERITS) {
						$topic->owner_validation_date = $topic->parent->owner_validation_date;
					} else {
						$topic->owner_validation_date = null;
					}

					if ($topic->owner_validation != ProjectTopic::YES && $topic->adviser_validation != ProjectTopic::YES){
						$topic->validation_comment = null;
					}

					Yii::log("Saving item {$topic->key} with id {$topic->id} on project {$topic->project_id}.", CLogger::LEVEL_INFO, $category);
					$update = $update & $topic->save();
					foreach ($topic->children as $child) {
						$child->setParent($topic);
						$stack[] = $child;
					}
				}
			}
		}

		if (!$update) {
			$this->controller->render('objectives', array (
				'model' => $meeting,
				'topics' => $topics
			));
		} else {
			$this->controller->redirect(array_merge(array('itemTree'), $_GET));
		}
	}
}